#!/usr/bin/env bash
# shellcheck disable=SC1090
userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap

if [ -f $sysresources ]; then
	xrdb -merge $sysresources
fi

if [ -f $sysmodmap ]; then
	xmodmap $sysmodmap
fi

if [ -f "$userresources" ]; then
	xrdb -merge "$userresources"
fi

if [ -f "$usermodmap" ]; then
	xmodmap "$usermodmap"
fi

if [ -d /etc/X11/xinit/xinitrc.d ]; then
	for f in /etc/X11/xinit/xinitrc.d/?*.sh; do
		[ -x "$f" ] && . "$f"
	done
	unset f
fi

has_cmd() {
	command -v "$1" >/dev/null
}

cmds=(
	lxqt-session xfce4-session mate-session openbox-session fluxbox gnome-session
	fluxbox
)
for cmd in "${cmds[@]}"; do
	if has_cmd "$cmd"; then
		break
	fi
done

if [ -n "$XDG_CURRENT_DESKTOP" ]; then
	echo "$cmd"
else
	if [ "$cmd" = fluxbox ]; then
		fluxbox-generate_menu
	fi
	"$cmd" &
fi
